<?php
namespace Home\Action;
use Common\Action\FirstendAction;
use Common\Model\userModel;
use Common\Model\navModel;
class BaseAction extends FirstendAction{
public function _initialize()
    {
	parent::_initialize();
	$this->assign('navlist',$this->nav());
	$this->tqkuid=C('yh_app_kehuduan');
	$this->assign('hotkey',F('data/hotkey'));
	$this->assign('trackurl',$this->trackid());
	$tkapi=trim(C('yh_api_line'));
	if(false ===$tkapi){
		$this->tqkapi = 'http://api.tuiquanke.com';
		}else{
		$this->tqkapi = $tkapi;
	}
	$this->assign('alertwin',$this->_alert_adv());
	}

	
 protected function _alert_adv(){
 	$Ad = S('alertadhome');
    if($Ad){
    	$adlist = $Ad;
    }else{
     $adlist = M('ad')->field('id',true)->where('status=3 and add_time=0')->find();
      S('alertadhome',$adlist,3600);
     }
      return $adlist;
    }	

protected function nav(){
$where=array(
'status'=>1,
'type'=>'main'
);
$navdata = S('navdata');
if($navdata){
$navlist	 = $navdata;
}else{
	
$mod=new navModel();
$navlist=$mod->cache(true, 50 * 60)->field('name,alias,link,target')->where($where)->order('ordid asc,id desc')->select();

}

return $navlist;

}

    protected function trackid(){
        $track=I('t','0','number_int');
        $isagent=cookie('createagent');
        
    if($this->visitor->is_login){
	$webmaster=$this->visitor->get();
	if($webmaster['tbname']==1){
	$data=array(
    	't_pid'=>$webmaster['webmaster_pid'],
    	'p_pid'=>$webmaster['pdd_pid'],
    	'recode'=>$webmaster['invocode'],
    	't'=>$webmaster['id'],
    	);
    	 $data=serialize($data);
    	 cookie("trackid", $data, 86400 * 15);
     $trackurl='?t='.$webmaster['id'];
      return $trackurl;
	}
	}
        
        if((!empty($track) && $track!=$isagent && is_numeric($track)) || (false === $isagent && !empty($track) && is_numeric($track))){
        $usermod=new userModel();
        	$res=$usermod->field('webmaster_pid,pdd_pid,invocode')->where(array('id'=>$track))->find();
        	if($res){
        	$data=array(
        	't_pid'=>$res['webmaster_pid'],
        	'p_pid'=>$res['pdd_pid'],
        	'recode'=>$res['invocode'],
        	't'=>$track,
        	);
        	$data=serialize($data);
       }
         cookie("trackid", $data, 86400 * 15);
         cookie('createagent',$track);
        }
        $trackid=cookie('trackid');
        if(!empty($trackid)){
        	$data=unserialize($trackid);
            $trackurl='?t='.$data['t'];
            return $trackurl;
        }
        

    }
	
	
	
}
	